﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl;
//using DeepEarth.Client.Services.GWC;
using Microsoft.Maps.MapControl.Core;

namespace SCA.Client.GWCLayers
{
    public class GWCLayers : IDisposable
    {
        Map _map;

        MapTileLayer tl3;


        public GWCLayers(Map map)
        {

        }

        public void ShowLayers(Map map)
        {
            if (null == _map)
                _map = map;

            //_map.ViewChangeEnd += _map_ViewChangeEnd;

            MapTileLayer tl = new MapTileLayer() { Name = "sverigekartan" };
            //tl.TileSources.Add(new GWCTileSourceBing("http://172.18.39.89:8080/geoserver/gwc", "sca:sverigekartan_wgs84"));
            //tl.TileSources.Add(new GWCTileSourceBing("http://janus.swecosundsvall.se:6336/geoserver/gwc", "kvarken_oil2"));
            //tl.TileSources.Add(new GWCTileSourceBing("http://sca-tiles.swecosundsvall.se/geoserver/gwc", "sca:sverigekartan_wgs84"));
            tl.Opacity = 0.8;
            _map.Children.Add(tl);

            MapTileLayer tl2 = new MapTileLayer() { Name = "terrängkartan" };
            //tl2.TileSources.Add(new GWCTileSourceBing("http://172.18.39.89:6336/geoserver/gwc", "sca:IntressantaVindomraaden_SCA_201002"));'
            //tl2.TileSources.Add(new GWCTileSourceBing("http://sca-tiles.swecosundsvall.se/geoserver/gwc", "sca:vnorrland"));
            tl2.Opacity = 0.8;
            _map.Children.Add(tl2);

            if (null == tl3)
            {
                tl3 = new MapTileLayer { Name = "scamark" };
                //tl3.TileSources.Add(new GWCTileSourceBing("http://sca-tiles.swecosundsvall.se/geoserver/gwc", "sca:sca_mark"));
                _map.Children.Add(tl3);
            }


        }

        void _map_ViewChangeEnd(object sender, MapEventArgs e)
        {
            if (_map.ZoomLevel > 8)
                SetOverlayVisibility("sverigekartan", Visibility.Collapsed);
            else
                SetOverlayVisibility("sverigekartan", Visibility.Visible);

            if (_map.ZoomLevel <= 8)
                SetOverlayVisibility("terrängkartan", Visibility.Collapsed);
            else
                SetOverlayVisibility("terrängkartan", Visibility.Visible);
        }

        public void ToggleFastighetsKarta(bool visible)
        {
            if (visible)
            {
                if (null != tl3)
                    SetOverlayVisibility("scamark", Visibility.Visible);
            }
            else
            {
                if (null != tl3)
                    SetOverlayVisibility("scamark", Visibility.Collapsed);
            }
        }

        void SetOverlayVisibility(string name, Visibility visibility)
        {
            MapTileLayer mtl = (MapTileLayer)_map.FindName(name);
            mtl.Visibility = visibility;
            return;

            //foreach (UIElement uie in _map.Children)
            //{
            //    if (uie is MapTileLayer)
            //    {
            //        MapTileLayer mtl = uie as MapTileLayer;
            //        if (mtl.Name == name)
            //        {
            //            uie.Visibility = visibility;
            //            return;
            //        }
            //    }
            //}
        }

        #region IDisposable Members

        public void Dispose()
        {
            _map.ViewChangeEnd -= _map_ViewChangeEnd;
        }

        #endregion
    }

    public class MyCustomTMS : Microsoft.Maps.MapControl.TileSource
    {
        public override Uri GetUri(int x, int y, int zoomLevel)
        {
            return base.GetUri(x, y, zoomLevel);
        }
    }
}
